$(document).ready(function(){
	$('#dialog_add_role').click(function()
			{				
				$.post("/categories/addrole",{
						'id_res' : $("#id").val(),
						'id_role' : $('#dialog_id_role option:selected').val()
				});
				
				var str = $("#id_role").html();
				str += '<option value="'+$('#dialog_id_role option:selected').val()+'">'+$('#dialog_id_role option:selected').text()+'</option>';				
				$("#id_role").html(str);
				$('#dialog_id_role option:selected').remove();
	});
	
	
			$('#remove_role').click(function()
			{
				if(confirm('Вы действительно хотите удалить это роль?'))
				{
					jQuery.post("/categories/removerole", {'id_res' : jQuery("#id").val(), 'id_role' : jQuery('#id_role option:selected').val()});
					jQuery('#id_role option:selected').remove();
				}
			});	
			
			$('#rules_all_allow').click(function()
			{				
				if(!$('#rules_all_allow').attr('checked'))
				 {
					 $('#rules_list input.allow').attr('checked', '');
				 }
				 else
				 {
					 $('#rules_list input.allow').attr('checked', 'checked');					 
					 $('#rules_list input.deny').attr('checked', '');									 
				 }
				 $('#apply_rule').attr('disabled', '');
			});	
			
			$('#rules_all_deny').click(function()
			{				
				if(!$('#rules_all_deny').attr('checked'))
				 {
					 $('#rules_list input.deny').attr('checked', '');				 	
				 }
				 else
				 {
					 $('#rules_list input.allow').attr('checked', '');					 
					 $('#rules_list input.deny').attr('checked', 'checked');
				 }
				 $('#apply_rule').attr('disabled', '');
			});			
			
			$('#id_role').click(function()
			{				
				if(!(parseInt($('#id_role option:selected').val()) > 0)) return false;
				
				$('#rules_loader').show();
				$('#remove_role').attr('disabled', '');
				$('#rules_list input').attr('checked', '');				
				
				$.getJSON("/categories/getrule", {'id_res' : $("#id").val(), 'id_role' : $('#id_role option:selected').val()},
			        function(data){
				         $.each(data, function(i, id){           						         	
				         	 switch(id){
				         	 	case 'rules_all_allow':
				         	 	    $('#rules_list input.allow').attr('checked', 'checked');
				         	 		break;
				         	 	case 'rules_all_deny':
				         	 	    $('#rules_list input.deny').attr('checked', 'checked');
				         	 		break;	
				         	 	default:				         	 	    
				         	 	    $('#'+id).attr('checked', 'checked');
				         	 }
				         });				     
				         $('#rules_loader').hide();    
				         $('#rules').show();
			        });				
			});
			
			$('#rules_list input').click(function()
			{
				var id = $(this).attr('id');
				if(id != 'rules_all_allow' && id != 'rules_all_deny')
				{
					$('#rules_all_allow').attr('checked', ''); 
					$('#rules_all_deny').attr('checked', '');
					
					id_arr = id.split('_');										
					uid = id_arr[0]+'_'+id_arr[1]+'_'+(id_arr[2] == 'allow' ? 'deny' : 'allow');					
					
					if($(this).attr('checked'))						
						$('#'+uid).attr('checked', '');					
					else					
						$('#'+uid).attr('checked', 'checked');					
				}
				$('#apply_rule').attr('disabled', '');			
			});			

			$('#apply_rule').click(function()
			{
				$('#rules_apply_loader').show();				
								
				var allow = '';
				var deny = '';
				
				$('#rules_list input:checked').each(function(){
					if($(this).attr('class') == 'allow') allow +=jQuery(this).val()+';';
					else deny +=jQuery(this).val()+';';
				});	
				
				if($('#rules_all_allow').attr('checked'))	allow = '-1;';
				if($('#rules_all_deny').attr('checked'))	deny = '-1;';				
				
				$.post("/categories/applyrule", 
					{
						'resource_id' : $("#id").val(),
						'role_id' : $('#id_role option:selected').val(),
						'allow' : allow,
						'deny' : deny
					}, function(){$('#rules_apply_loader').hide();});
			});	
});



function showRoles()
{	
    var data = Array();
    $("#id_role option").each(function()
    {			        
    	data.push($(this).val());
    });        
    
	$("#dialog_id_role").load("/categories/ajax-role-list", {'a' : data.toString()});	
	return hs.htmlExpand(null, {contentId:'popup_roles', headingText: 'Роли пользователя'});		
}

function showAddRule()
{
	return hs.htmlExpand(null, {contentId:'popup_add_rule', headingText: 'Добавить правило'});
}

function addRule(id, name, name_full)
{
	$.post("/categories/ajax-add-priv", 
			{
				'id' : id,				
				'name' : name,
				'name_full' : name_full
			}, 
			function(data){
				if(parseInt(data) > 0) 
				{
					var str = '<tr id="priv'+data+'"><td width="40%">'+name_full+'</td><td align="center"><input type="checkbox" class="allow" id="rules_tags_allow" value="'+data+'" name="allow"></td><td align="center"><input type="checkbox" class="deny" id="rules_tags_deny" value="'+data+'" name="deny"></td><td align="center"><a href="javascript:;" onclick="removePriv('+data+')"><img src="/images/cross.png"/></a></td></tr>';												
					$('#rules_list tr:last').after(str);
					$('#rules_list').scrollTo('tr:last');
					hs.close();
				}
				else alert(data);
			});
}

function removePriv(id)
{
	$.post("/categories/ajax-remove-priv", 
			{
				'id' : id				
			}, 
			function(data){
				if(data == 'OK') $('#priv'+id).remove();
				else alert(data);
			});
}
